import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/home_business/widget/collect_status_wrapper.dart';
import 'package:xvideos/hj_page/home_business/widget/user_name_view.dart';
import 'package:xvideos/hj_utils/api_service/mine_service.dart';
import 'package:xvideos/hj_utils/date_time_util.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/routers/jump_router.dart';
import 'package:xvideos/tools_base/image/custom_network_image.dart';

import '../../../tools_base/widget/net_image_widget.dart';
import '../../community_business/widget/follow_loading_button.dart';

class PostUserInfoView extends StatefulWidget {
  final VideoModel model;
  final bool isDarkStyle; // 暗网模式
  final bool isDetailStyle; // 帖子详情模式
  final bool isEditStatus; // 编辑模式
  final double? avatarSize;

  const PostUserInfoView({
    super.key,
    required this.model,
    this.isDarkStyle = false,
    this.avatarSize,
    this.isDetailStyle = false,
    this.isEditStatus = false,
  });

  @override
  State<StatefulWidget> createState() {
    return _PostUserInfoViewState();
  }
}

class _PostUserInfoViewState extends State<PostUserInfoView> {
  VideoModel? get model => widget.model;
  bool _isFollowLoading = false;

  @override
  Widget build(BuildContext context) {
    if (model == null || model?.publisher == null || model?.newsType == 'ADULT_GAME') return SizedBox.shrink();
    return Row(
      children: [
        InkWell(
          onTap: () {
            pushToPersonCenter(model?.publisher?.uid);
          },
          child: NetworkImageLoader(
            imageUrl: model?.publisher?.portrait ?? "",
            width: 40,
            height: 40,
            borderRadius: 20,
          ),
        ),
        10.sizeBoxW,
        Expanded(
          child: widget.isDetailStyle ? _buildNameDetail() : _buildNameNormal(),
        ),
        if (widget.isDetailStyle)
          CollectStatusWrapper(
            videoModel: model,
            builder: () {
              return InkWell(
                onTap: () => _followEvent(),
                child: FollowLoadingButton(
                  isFollow: model?.publisher?.hasFollowed ?? false,
                  isLoading: _isFollowLoading,
                  isDarkStyle: widget.isDarkStyle,
                ),
              );
            },
          ),
      ],
    );
  }

  void _followEvent() async {
    if (_isFollowLoading) return;
    _isFollowLoading = true;
    final res = await MineService.getFollow(model?.publisher?.uid, !(model?.publisher?.hasFollowed ?? false));
    if (res) {
      model?.publisher?.hasFollowed = !(model?.publisher?.hasFollowed ?? false);
    }

    _isFollowLoading = false;
    if (mounted) {
      setState(() {});
    }
  }

  Widget _buildNameNormal() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        UserNameView(
          nameColor: Color(0xFFFFFFFF),
          name: model?.publisher?.name,
          isOfficial: model?.publisher?.officialCert,
        ),
        2.sizeBoxH,
        Text(
          '${formatTimeTwo(model?.reviewAt)}',
          style: TextStyle(color: Color(0xff999999), fontSize: 12),
        ),
      ],
    );
  }

  Widget _buildNameDetail() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Flexible(
              child: UserNameView(
                nameColor: Color(0xFFFFFFFF),
                name: model?.publisher?.name,
                isOfficial: model?.publisher?.officialCert,
              ),
            ),
            if (model?.publisher?.officialCert == true) ...[4.sizeBoxW, Image.asset("official_icon.png".commonImgPath, width: 22, height: 22)],
          ],
        ),
        2.sizeBoxH,
        Text(
          '${formatTimeTwo(model?.reviewAt)}',
          style: TextStyle(color: Color(0xff999999), fontSize: 12),
        ),
      ],
    );
  }
}
